﻿#region

using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Net.Http;
using System.Security.Cryptography;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using CuongHanh.Models;
using CuongHanh.Models.View;

#endregion

namespace CuongHanh.Context
{
    public static class ExtensionMethods
    {
        public static string ToUrlName(this string text)
        {
            return BoDauTiengViet.Singleton.ToUrlName(text);
        }

        public static string GetImage(this UrlHelper url,
                string imageName,
                ImageType imageType)
        {
            switch (imageType)
            {
                case ImageType.Thumnail:
                    return url.Content(Path.Combine(AppUtil.THUMNAIL_IMAGE_PATH,
                            imageName ?? ""));
                case ImageType.Small:
                    return url.Content(Path.Combine(AppUtil.SMALL_IMAGE_PATH,
                            imageName ?? ""));
                case ImageType.Large:
                    return url.Content(Path.Combine(AppUtil.LARGE_IMAGE_PATH,
                            imageName ?? ""));
            }
            return string.Empty;
        }


        

        public static string DrawImage(this HttpContext context,
              int styleMaterialId)
        {
            using (Bitmap image = new Bitmap(context.Server.MapPath("images/stars_5.png")))
            {
                using (MemoryStream ms = new MemoryStream())
                {
                    image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                    ms.WriteTo(context.Response.OutputStream);
                }
            }
            return string.Empty;
        }




        /// <summary>
        /// todo using for gem
        /// </summary>
        /// <param name="url"></param>
        /// <param name="imageName"></param>
        /// <returns></returns>
        public static string GetImage(this UrlHelper url,
                string imageName)
        {
            
                    return url.Content(Path.Combine(AppUtil.GEM_IMAGE_PATH,
                            imageName ?? ""));
        }




        public static string GetMd5(this string text)
        {
            // step 1, calculate MD5 hash from input
            MD5 md5 = MD5.Create();
            byte[] inputBytes = Encoding.ASCII.GetBytes(text);
            byte[] hash = md5.ComputeHash(inputBytes);

            // step 2, convert byte array to hex string
            StringBuilder sb = new StringBuilder();
            foreach (byte t in hash)
            {
                sb.Append(t.ToString("X2"));
            }
            return sb.ToString();
        }

        #region Format

        public static string FormatNumber(this long value)
        {
            return string.Format("{0:#,###}",
                    value);
        }

        public static string FormatNumber(this int value)
        {
            return string.Format("{0:#,###}",
                    value);
        }

        public static string FormatNumber(this double value)
        {
            return string.Format("{0:#,###.00}",
                    value);
        }

        #endregion

        #region SubCategory

        public static string GetSubCategoryName(int id,
                string l)
        {
            return AppUtil.SubCategoryName(CacheContext<SubCategory>.Singleton.GetObject(id),
                    l);
        }

        #endregion

        #region Url

      
        #endregion
    }

    public enum ImageType
    {
        Thumnail = 1,

        Small = 2,

        Large = 3
    }

    public enum UrlType
    {
        Category = 1,

        SubCategory = 2,

        Product = 3,

        StyleType = 4,

        JavascriptVoid0 = 10
    }
}
